package com.modifiertech.lineage.commands;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerDataHandler;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CmdAttributeModify extends CommandBase {

    @Override
    public String getName() {
        return "lineageattr";
    }

    @Override
    public String getUsage(ICommandSender iCommandSender) {
        return "lineage.cmdattributemodify.useage";
    }

    @Override
    public void execute(MinecraftServer minecraftServer, ICommandSender iCommandSender, String[] strings) throws CommandException {
        if (strings.length == 0) {
            throw new CommandException("cmdattributemodify.invalid.missing.playername");
        }
        if (strings.length == 1) {
            throw new CommandException("cmdattributemodify.invalid.missing.attrtype");
        }
        if(strings.length==2){
            throw new CommandException("cmdattributemodify.invalid.missing.values");
        }
        //                       0       1         2       3
        //lineageattr  @p strength add 10
        String[] finalStrings = strings;
        boolean ifSilent=CmdHelper.ifExecAsSilent(strings);
        if(ifSilent)strings=CmdHelper.cutSilentCmd(strings);

        EntityPlayerMP entityPlayerMP=strings.length>1 ? CommandBase.getPlayer(minecraftServer,iCommandSender,strings[0])
                :CommandBase.getCommandSenderAsPlayer(iCommandSender);
        //属性类型
        String attrNameTag="-";
        if(BaseConst.Attr.list.stream()
                .filter(h->h.name().equals(finalStrings[1])).findAny().isPresent()){
            attrNameTag=strings[1];
        }else{
            throw new CommandException("cmdattributemodify.invalid.attrtype.values");
        }
        //操作类型
        String attropttype="-";
        if(BaseConst.AttrOperate.list.stream()
            .filter(h->h.name().equals(finalStrings[2])).findFirst().isPresent()){
            attropttype=strings[2];
        }else{
            throw new CommandException("cmdattributemodify.invalid.opttype.values");
        }
        //操作值
        int nums=0;
        try{
            nums=Integer.parseInt(strings[3]);
            if(nums<0)throw new Exception("-");
        }catch (Exception e){
            throw new CommandException("cmdattributemodify.invalid.optvalue.values");
        }
        PlayerData data = PlayerDataHandler.get(entityPlayerMP);
        data.optAttr(attrNameTag,attropttype,nums);
        data.saveAndSync();
        CmdHelper.execServerSay(new TextComponentTranslation("lineage.cmd.attrmodify.msg",strings[0],attrNameTag,attropttype,nums));
    }
    @Nonnull
    @Override
    public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
        if (args.length == 1) {
            return Arrays.asList(server.getPlayerList().getOnlinePlayerNames());
        }
        if (args.length == 2) {
            String attrname = args[1];
            return BaseConst.Attr.list.stream().filter(name -> name.name().startsWith(attrname))
                    .map(BaseConst.Attr::name)
                    .collect(Collectors.toList());
        }
        if (args.length == 3) {
            String optname = args[2];
            return BaseConst.AttrOperate.list.stream().filter(name -> name.name().startsWith(optname))
                    .map(BaseConst.AttrOperate::name)
                    .collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
